#!/usr/bin/env python
# -*- coding: utf-8 -*-

"Test PyGTK"

import pygtk
pygtk.require('2.0')
import gtk
from CM110 import CM110

class StreamLike(object): # pylint: disable=R0903
    "A stream-like class to show logs in a window"

    def __init__(self, textview):
        self.textview = textview

    def write(self, message):
        "Add a message into the widget buffer"
        buff = self.textview.get_buffer()
        buff.insert(buff.get_end_iter(), message)
        self.textview.scroll_mark_onscreen(buff.get_insert())

##    def flush(self):
##        "Required by API"
##        pass

class PyApp(gtk.Window): # pylint: disable=R0904,R0924
    "Try to make a window"

    def __init__(self):
        gtk.Window.__init__(self)
        self.set_title("CM110 Control")
        self.cm110 = CM110()

        self.vbox1 = gtk.VBox()

        # Prepare First two lines of widgets
        self.vbox1.hbox1 = self.setup_hbox1()
        self.vbox1.scrolledwindow1 = self.setup_scrolledwindow1()
        self.cm110.set_log(StreamLike(self.vbox1.scrolledwindow1.textview1))

        self.vbox1.pack_start(self.vbox1.hbox1, expand=False, fill=False)
        self.vbox1.pack_start(self.vbox1.scrolledwindow1, expand=True,
                                                            fill=True)
        self.add(self.vbox1)

        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def setup_hbox1(self):
        "Create hbox1 and its internals"
        hbox1 = gtk.HBox()
        hbox1.label1 = gtk.Label(" Wavelength:")
        adjustment = gtk.Adjustment(self.cm110.state["position"], 0, 99000)
        hbox1.spinbutton1 = gtk.SpinButton(adjustment, 0, 0)
        hbox1.spinbutton1.set_numeric(True)
        hbox1.spinbutton1.update()
        adjustment.connect("value_changed", self.wavelength, hbox1.spinbutton1)
        hbox1.combobox1 = gtk.combo_box_new_text()
        for txt in ("10nm", "nm", "Å"):
            hbox1.combobox1.append_text(txt)
        hbox1.combobox1.set_active(self.cm110.state["units"])
        hbox1.combobox1.connect("changed", self.units)
        hbox1.button3 = gtk.Button("State")
        hbox1.button3.connect("clicked", self.get_state)
        hbox1.button4 = gtk.Button("Save NOVRAM")
        hbox1.button4.connect("clicked", self.save_NOVRAM)

        hbox1.label2 = gtk.Label("Grating:")
        hbox1.combobox2 = gtk.combo_box_new_text()
        hbox1.combobox2.append_text("1")
        if self.cm110.state["num_of_gratings"] == 2:
            hbox1.combobox2.append_text("2")
        hbox1.combobox2.set_active(int(self.cm110.state["grating"])-1)
        hbox1.combobox2.connect("changed", self.set_grating)
        grating = "%d gr/mm, blaze %d nm." % (self.cm110.state["grooves"],
                                              self.cm110.state["blaze"])
        hbox1.label3 = gtk.Label(grating)

        hbox1.button5 = gtk.Button("Close")
        hbox1.button5.connect("clicked", gtk.main_quit)

        hbox1.pack_start(hbox1.label2, expand=False, fill=False)
        hbox1.pack_start(hbox1.combobox2, expand=False, fill=False)
        hbox1.pack_start(hbox1.label3, expand=False, fill=False)
        hbox1.pack_start(hbox1.label1, expand=False, fill=False)
        hbox1.pack_start(hbox1.spinbutton1, expand=False, fill=False)
        hbox1.pack_start(hbox1.combobox1, expand=False, fill=False)
        hbox1.pack_start(hbox1.button3, expand=False, fill=False)
        hbox1.pack_end(hbox1.button5, expand=False, fill=False)
        hbox1.pack_end(hbox1.button4, expand=False, fill=False)
        return hbox1

    def setup_scrolledwindow1(self): # pylint: disable=R0201
        "Create scrolled text window"
        scrolledwindow1 = gtk.ScrolledWindow()
        scrolledwindow1.textview1 = gtk.TextView()
        scrolledwindow1.textview1.set_cursor_visible(False)
        scrolledwindow1.textview1.set_editable(False)
        scrolledwindow1.add(scrolledwindow1.textview1)
        return scrolledwindow1

    def get_state(self, _widget=None):
        "Add some text to a buffer"
        self.cm110.get_state()
        self.vbox1.hbox1.spinbutton1.set_value(self.cm110.state["position"])
        self.vbox1.hbox1.combobox1.set_active(self.cm110.state["units"])
        grating = "%d gr/mm, blaze %d nm." % (self.cm110.state["grooves"],
                                              self.cm110.state["blaze"])
        self.vbox1.hbox1.label3.set_text(grating)

    def units(self, widget):
        "A call-back for units change"
        self.cm110.units(widget.get_active_text())
        self.vbox1.hbox1.spinbutton1.set_value(0) # It also resets wavelength

    def wavelength(self, _widget, spin):
        "A call-back for units change"
        self.cm110.goto(spin.get_value_as_int())

    def save_NOVRAM(self, _widget): # pylint: disable=C0103
        "A call-back saving memory to a file"
        self.cm110.save_NOVRAM()

    def set_grating(self, widget):
        "A call-back to select a grating"
        self.cm110.select(widget.get_active_text())
        self.get_state() # reflect the state change by re-reading the state

PYAPP = PyApp()
gtk.main()
